local assigned = redis.call("hget", "leastKeyAssignMap", KEYS[1])
if assigned == false then
    local card = redis.call("zcard", "leastKeyMap")
    if card < tonumber(KEYS[2]) then
        redis.call("zadd", "leastKeyMap", 1, card)
        redis.call("hset", "leastKeyAssignMap", KEYS[1], card)
        return card
    else
        local min = redis.call("zrange", "leastKeyMap", 0, 0)
        redis.call("zincrby", "leastKeyMap", 1, min[1])
        redis.call("hset", "leastKeyAssignMap", KEYS[1], min[1])
        return tonumber(min[1])
    end
else
    return tonumber(assigned)
end
